﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.UnitTest
{
    public class ApplicationSecurityManager : IDisposable
    {
        //http://localhost/SecurityApi/api/UserApplication/?userName=user1@codeplex.com&enterpriseId=1
        const string APP_URI = @"/api/UserApplication/?userName={0}&enterpriseId=1";
        const string LOGIN_URI = @"/api/User/VerifyUser/?userName={0}&password={1}&enterpriseId=1";
        const string APP_BY_ID = @"/api/Application/{0}";

        /// <summary>
        /// Talks to Security service using REST to get list of applications
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<List<SecurityApi.Model.Application>> GetApplicationModules(string userName)
        {
            string address = GetBaseAddress() + string.Format(APP_URI, userName);

            // Create an HttpClient instance
            HttpClient client = new HttpClient();

            // Send a request asynchronously continue when complete
            HttpResponseMessage response = await client.GetAsync(address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();

            string strResponse = await response.Content.ReadAsStringAsync();
            List<SecurityApi.Model.Application> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SecurityApi.Model.Application>>(strResponse);
            return list;
        }


        public void Dispose()
        {
        }

        private string GetBaseAddress()
        {
            string baseAddress = string.Empty;
            string key = @"SecurityApiBaseAddress";

            if (null != ConfigurationManager.AppSettings[key])
            {
                baseAddress = ConfigurationManager.AppSettings[key].ToString();
            }

            return baseAddress;
        }

        public async Task<SecurityApi.Model.Application> GetApplicationById(int applicationId)
        {
            string address = GetBaseAddress() + string.Format(APP_BY_ID, applicationId);

            // Create an HttpClient instance
            HttpClient client = new HttpClient();

            // Send a request asynchronously continue when complete
            HttpResponseMessage response = await client.GetAsync(address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();

            string strResponse = await response.Content.ReadAsStringAsync();
            SecurityApi.Model.Application result = Newtonsoft.Json.JsonConvert.DeserializeObject<SecurityApi.Model.Application>(strResponse);
            return result;

        }

        public async Task<bool> VerifyUser(string userName, string password, int enterpriseId)
        {
            string address = GetBaseAddress() + string.Format(LOGIN_URI, userName, password);

            // Create an HttpClient instance
            HttpClient client = new HttpClient();

            // Send a request asynchronously continue when complete
            HttpResponseMessage response = await client.GetAsync(address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();

            string strResponse = await response.Content.ReadAsStringAsync();
            bool result = Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(strResponse);
            return result;
        }

    }
}
